<?php
class Topic_model extends CI_Model{
	function __construct(){
		parent::__construct();
	}

	public function action($pid){
		
		$data['pid'] = $pid;
		$data['uid'] = $this->auth->uid;
		$data['uname'] = $this->auth->uname;
		$data['content'] = $this->form_validation->set_value('content');
		$data['point_type'] = $this->form_validation->set_value('point_type');
		$data['time'] = date('Y-m-d H:i:s');
		if($this->db->insert('zixun',$data)){
			return array('status'=>true);
		}else{
			return array('status'=>false,'msg'=>'评论失败，可能网络出错！');
		}
	}


	public function get_topic($pid,$start=0,$limit=null){
		
		$this->db->where('pid',$pid);
		$this->db->order_by('time','DESC');
		$this->db->limit($limit,$start);

		$query = $this->db->get('topic');
		if($query->num_rows){
			return $query->result_id();
		} else {return false;}
	}

	public function get_topic_reply($tid,$start=0,$limit=null){
		
		$this->db->where('tid',$tid);
		$this->db->order_by('time','ASC');
		$this->db->limit($limit,$start);

		$query = $this->db->get('topic_reply');
		if($query->num_rows){
			return $query->result_id();
		} else {return false;}
	}
	public function get_topic_byid($id){
		
		$this->db->where('id',$id);

		$query = $this->db->get('topic');
		if($query->num_rows){
			return $query->row();
		} else {return false;}
	}

	public function get_topic_num($pid){
		$this->db->where('pid',$pid);
		return $this->db->count_all_results('topic');
	}
	public function get_topic_reply_num($tid){
		$this->db->where('tid',$tid);
		return $this->db->count_all_results('topic_reply');
	}

	public function add_topic($pid,$title,$content){
		$data['content'] = $content;
		$data['title'] = $title;
		$data['uid'] = $this->auth->uid;
		$data['uname'] = $this->auth->uname;
		$data['pid'] = $pid;
		$data['time'] = date('Y-m-d H:i:s');
		if($this->db->insert('topic',$data)){
			return array('status'=>true,'tid'=>$this->db->insert_id());
		}else{
			return array('status'=>false,'msg'=>'回复失败，可能网络出错！');
		}
		
	}
	public function add_reply($tid){
		$data['tid'] = $tid;
		$data['uid'] = $this->auth->uid;
		$data['uname'] = $this->auth->uname;
		$data['content'] = $this->form_validation->set_value('content');
		$data['time'] = date('Y-m-d H:i:s');
		if($this->db->insert('topic_reply',$data)){
			return array('status'=>true);
		}else{
			return array('status'=>false,'msg'=>'评论失败，可能网络出错！');
		}
		
	}
}